package com.dy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.entity.Cinema;
import com.dy.entity.Order;
import com.dy.mapper.CinemaMapper;
import com.dy.mapper.OrderMapper;
import com.dy.service.IFeignOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/cinema")
public class OrderController {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CinemaMapper cinemaMapper;
    @Qualifier("com.dy.service.IFeignOrderService")
    @Autowired
    private IFeignOrderService feignOrderService;

    @PostMapping("/deadOrder")
    public void getOrder(@RequestBody Order order){
        //判断冥等性问题
        QueryWrapper<Order> orderWrapper = new QueryWrapper<>();
        orderWrapper.eq("order_serial",order.getOrderSerial());
        Order order1 = orderMapper.selectOne(orderWrapper);
        if (order1 == null){
            QueryWrapper<Cinema> wrapper = new QueryWrapper<>();
            wrapper.eq("cinema_name",order.getCinameName());
            Cinema cinema = cinemaMapper.selectOne(wrapper);
            cinema.setCinemaNum(cinema.getCinemaNum()-order.getOrderNumber());
            cinemaMapper.update(cinema,wrapper);
            if (orderMapper.insert(order) > 0 ? true : false){
                feignOrderService.updateOrderState(order.getOrderSerial());
            }
        }
    }
}
